<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="6" >
            <a-form-model-item label="单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="code">
              <a-input v-model="model.code" placeholder="请输入单号" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="来源编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="def3">
              <j-popup
                v-model="model.def3"
                field="saleOrderId"
                org-fields="id,order_code"
                dest-fields="saleOrderId,def3"
                code="ces_order_main"
                :multi="false"
                @input="popupCallback"
              />
<!--              <a-input v-model="model.def3" disabled></a-input>-->
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="客户" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="def1">
              <j-popup
                v-model="model.def1"
                field="customerId"
                org-fields="id,name"
                dest-fields="customerId,def1"
                code="SelectorCustomer"
                :multi="false"
                @input="popupCallback"
              />

            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="业务员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="def2">
              <j-popup
                v-model="model.personId"
                field="personId"
                org-fields="id,username"
                dest-fields="personId,def2"
                code="SelectorUser"
                :multi="false"
                @input="popupCallback"
              />
<!--              <a-input v-model="model.def2" placeholder="" disabled></a-input>-->
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="收款日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="receiptDate">
              <a-input v-model="model.receiptDate" placeholder="请输入收款日期" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="收款金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="totalAmount">
              <a-input v-model="model.totalAmount" placeholder="请输入收款金额" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="description">
              <a-input v-model="model.description" placeholder="请输入备注" ></a-input>
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="6" >-->
<!--            <a-form-model-item label="审核人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approveBy">-->
<!--              <a-input v-model="model.approveBy" placeholder="请输入审核人" ></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="6" >-->
<!--            <a-form-model-item label="审核时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approveTime">-->
<!--              <a-input v-model="model.approveTime" placeholder="请输入审核时间" ></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
          <a-col :span="6" >
            <a-form-model-item label="单据状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="actstatus">
              <j-dict-select-tag v-model="model.actstatus" dictCode="spzt"  placeholder="请输入单据状态" style="width: 100%" disabled/>
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="6" >-->
<!--            <a-form-model-item label="驳回原因" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="refuseReason">-->
<!--              <a-input v-model="model.refuseReason" placeholder="请输入驳回原因" disabled></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
          <a-col :span="6" >
            <a-form-model-item label="支付状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="paymentStatus">
              <j-dict-select-tag v-model="model.paymentStatus" dictCode="sczt" placeholder="请输入支付状态" style="width: 100%" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="自定义项1" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="def1" v-if="false">
              <a-input v-model="model.def1" placeholder="请输入自定义项1" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="自定义项2" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="def2" v-if="false">
              <a-input v-model="model.def2" placeholder="请输入自定义项2" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="自定义项3" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="def3" v-if="false">
              <a-input v-model="model.def3" placeholder="请输入自定义项3" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="自定义项4" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="def4" v-if="false">
              <a-input v-model="model.def4" placeholder="请输入自定义项4" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="自定义项5" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="def5" v-if="false">
              <a-input v-model="model.def5" placeholder="请输入自定义项5" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="自定义项6" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="def6" v-if="false">
              <a-input v-model="model.def6" placeholder="请输入自定义项6" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="自定义项7" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="def7" v-if="false">
              <a-input v-model="model.def7" placeholder="请输入自定义项7" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="自定义项8" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="def8" v-if="false">
              <a-input v-model="model.def8" placeholder="请输入自定义项8" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="自定义项9" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="def9" v-if="false">
              <a-input v-model="model.def9" placeholder="请输入自定义项9" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="自定义项10" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="de10" v-if="false" >
              <a-input v-model="model.de10" placeholder="请输入自定义项10" ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
      <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="收款单子表" :key="refKeys[0]" :forceRender="true">
        <nbcio-editable-table
          keep-source
          :ref="refKeys[0]"
          :loading="erpReceiptDetailTable.loading"
          :columns="erpReceiptDetailTable.columns"
          :dataSource="erpReceiptDetailTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :toolbar="true"
          :actionButton="true"
          >
          </nbcio-editable-table>
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>

  import { getAction,putAction } from '@/api/manage'
  import { JVxeTableModelMixin } from '@/mixins/JVxeTableModelMixin.js'
  import { JVXETypes } from '@/components/jeecg/JVxeTable'
  import { NbcioTableModelMixin } from '@/mixins/NbcioTableModelMixin'
  import { getRefPromise,VALIDATE_FAILED} from '@/components/jeecg/JVxeTable/utils/vxeUtils.js'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  const ruleBaseURL = '/sys/fillRule/executeRuleByCode/'
  import { GoodsModalMixin } from '../mixins/GoodsModalMixin.js'
  export default {
    name: 'ErpReceiptForm',
    mixins: [JVxeTableModelMixin],
    components: {
      JFormContainer,
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        model:{
         },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
        },
        refKeys: ['erpReceiptDetail', ],
        tableKeys:['erpReceiptDetail', ],
        activeKey: 'erpReceiptDetail',
        // 收款单子表
        erpReceiptDetailTable: {
          loading: false,
          dataSource: [],
          columns: [
            // {
            //   title: '订单明细ID',
            //   key: 'saleOrderDetailId',
            //    type: JVXETypes.input,
            //   width:"200px",
            //   placeholder: '请输入${title}',
            //   defaultValue:'',
            // },
            {
              title: '商品名称',
              key: 'def3',
              type: JVXETypes.popup,
              popupCode:"SelectorGoods",
              field:"id,name",
              orgFields:"id,name",
              destFields:"def4,def3",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '数量',
              key: 'def1',
              type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '金额',
              key: 'def2',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '总价',
              key: 'def5',
              type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '税率(%)',
              key: 'def6',
              type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '税额',
              key: 'def7',
              type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '含税单价',
              key: 'def8',
              type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '无税单价',
              key: 'def9',
              type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },

            // {
            //   title: '金额',
            //   key: 'amount',
            //    type: JVXETypes.input,
            //   width:"200px",
            //   placeholder: '请输入${title}',
            //   defaultValue:'',
            // },

            {
              title: '备注',
              key: 'description',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        url: {
          rule: {
          // 编码规则校验 填写自己的编码规则
            orderCode: ruleBaseURL + 'ces_order_rule'
          },
          add: "/receipt/erpReceipt/add",
          edit: "/receipt/erpReceipt/edit",
          queryById: "/receipt/erpReceipt/queryById",
          erpReceiptDetail: {
            list: '/receipt/erpReceipt/queryErpReceiptDetailByMainId'
          },
        }
      }
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
    },
    methods: {
      getOrderCode() {
        putAction(this.url.rule.orderCode, this.model).then(res => {
          // 执行成功，获取返回的订单编号值，并赋到页面上
          if (res.success) {
            //赋值给自己定义的订单编号
            this.model.code = res.result
          }
        })
      },
      addBefore(){
      this.getOrderCode();
        this.erpReceiptDetailTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        this.$nextTick(() => {
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.erpReceiptDetail.list, params, this.erpReceiptDetailTable)
        }
      },
      //校验所有一对一子表表单
        validateSubForm(allValues){
            return new Promise((resolve,reject)=>{
              Promise.all([
              ]).then(() => {
                resolve(allValues)
              }).catch(e => {
                if (e.error === VALIDATE_FAILED) {
                  // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
                  this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
                } else {
                  console.error(e)
                }
              })
            })
        },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          erpReceiptDetailList: allValues.tablesValue[0].tableData,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
      popupCallback(value,row){
        console.log("popupCallback(value,row)",value,row);
        this.model = Object.assign(this.model, row);
        //this.checkSc();
      },

    }
  }
</script>

<style scoped>
</style>
